package com.example;

import org.junit.Test;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;

public class IntrospectorDemo {

    @Test
    public void test1() throws Exception {
        BeanInfo beanInfo = Introspector.getBeanInfo(Student.class);
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        System.out.println(propertyDescriptors.length);

        for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
            System.out.println(propertyDescriptor.getName());
        }
    }

    @Test
    public void test2() throws Exception {
        Student student = new Student();
        PropertyDescriptor propertyDescriptor = new PropertyDescriptor("name", Student.class);
        Method readMethod = propertyDescriptor.getReadMethod();
        String name = (String) readMethod.invoke(student, null);
        System.out.println(name);

        Method writeMethod = propertyDescriptor.getWriteMethod();
        writeMethod.invoke(student,"Alice");
        System.out.println(student.getName());
    }

}
